Imports org.codegen.win.controls
Imports org.model.lib.db

''' <summary>
''' Interface of classes that convert CriterionControl into a Windows 
''' Control
''' </summary>
Public Interface ICriterionControlConverter

    ''' <summary>
    ''' Function that converts a CriterionControl object reference 
    ''' into a list of Windows Controls
    ''' </summary>
    ''' <param name="c">CriterionMetadata object reference</param>
    ''' <returns>A list of controls</returns>
    ''' <remarks>
    ''' At least 2 controls are returned:  A label and an Input control
    ''' if isRange=true, then 4 controls are returned, to make an input
    ''' of 2 labels (From/To) and 2 Input controls (From/To)
    ''' </remarks>
    Function convert(ByVal c As CriterionMetadata) As List(Of Control)

End Interface

''' <summary>
''' Converts objects of type CriterionControl into Windows Controls
''' </summary>
Public Class CriterionControlConverter
    Implements ICriterionControlConverter

    Private Const STR_LBL1 As String = "lbl1"
    Private Const STR_LBL2 As String = "lbl2"


    Public Function convert(ByVal c As CriterionMetadata) As List(Of Control) _
                Implements ICriterionControlConverter.convert

        Dim ctrl As Control
        Dim ret As New List(Of Control)

        If c.isHidden Then
            ctrl = c.getUIControl
            ctrl.Visible = False
            ctrl.Name = c.Name
            'ctrl.Text = c.getDefaultValue 
            'change: 28/12/12: set the defalt values on the frmCriteria.
            'combo boxes do not work since they have no items loaded until
            'we add them to a container, like a form or control

            ret.Add(ctrl)
            If c.isRange Then
                ctrl = c.getUIControl
                ctrl.Name = c.Name2
                ret.Add(ctrl)

            End If

        Else

            Dim lbl As Label

            lbl = New Label
            lbl.Text = c.labelText
            lbl.AutoSize = True
            lbl.Dock = DockStyle.Fill
            lbl.Name = c.Name & STR_LBL1
            lbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight
            ret.Add(lbl)


            ctrl = c.getUIControl
            ctrl.Name = c.Name
            CType(ctrl, ICGBaseControl).AssociatedLabel = lbl
            CType(ctrl, ICGBaseControl).ReadOnly = c.isReadOnly
            CType(ctrl, ICGBaseControl).isMandatory = c.isMandatory
            '
            ctrl.Size = New Size(c.width, ctrl.Size.Height)
            ctrl.CausesValidation = True
            ret.Add(ctrl)

            If c.isRange Then
                lbl = New Label
                lbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight
                lbl.Text = c.labelText2
                lbl.Dock = DockStyle.Fill
                lbl.AutoSize = True
                lbl.Name = c.Name & STR_LBL2
                ret.Add(lbl)

                ctrl = c.getUIControl
                CType(ctrl, ICGBaseControl).AssociatedLabel = lbl
                CType(ctrl, ICGBaseControl).ReadOnly = c.isReadOnly2
                CType(ctrl, ICGBaseControl).isMandatory = c.isMandatory2
                '
                ctrl.Name = c.Name2
                ctrl.Size = New Size(c.width, ctrl.Size.Height)
                ctrl.CausesValidation = True
                ret.Add(ctrl)

            End If
        End If

        Return ret

    End Function

End Class